package com.bangda.crm.service.impl;

import java.util.List;

import com.bangda.crm.domain.enums.ClientStatusEnum;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bangda.crm.domain.CrmClientPhone;
import com.bangda.crm.mapper.CrmClientPoolMapper;
import com.bangda.crm.domain.CrmClientPool;
import com.bangda.crm.service.ICrmClientPoolService;

/**
 * 客户公海池Service业务层处理
 * 
 * @author fanboshi
 * @date 2023-10-15
 */
@Service
public class CrmClientPoolServiceImpl implements ICrmClientPoolService 
{
    @Autowired
    private CrmClientPoolMapper crmClientPoolMapper;

    @Autowired
    private ISysUserService userService;
//查询所有业务员名字
    @Override
    public List<String> getclientVestIn() {
        List<SysUser> sysUsers = userService.selectUserList(new SysUser());


        ArrayList<String> strings = new ArrayList<>();
        for (SysUser sysUser : sysUsers) {
            strings.add(sysUser.getNickName());
        }
        return strings;
    }

    @Override
    public long count(CrmClientPool crmClientPool) {
        return crmClientPoolMapper.count(crmClientPool);
    }

    /***
     * 查重
     * */
    @Override
    public List<CrmClientPool> chachong(CrmClientPool crmClientPool) {

        return crmClientPoolMapper.chachong(crmClientPool);
    }

    /**
     * 查询客户公海池
     * 
     * @param clientId 客户公海池主键
     * @return 客户公海池
     */
    @Override
    public CrmClientPool selectCrmClientPoolByClientId(Long clientId)
    {

        return crmClientPoolMapper.selectCrmClientPoolByClientId(clientId);
    }

    @Override
    public int CrmClientPool(CrmClientPool crmClientPool) {
        crmClientPool.setUpdateTime(DateUtils.getNowDate());
        return crmClientPoolMapper.updateCrmClientPool(crmClientPool);
    }

    /**
     * 查询客户公海池列表
     * 
     * @param crmClientPool 客户公海池
     * @return 客户公海池
     */
    @Override
    public List<CrmClientPool> selectCrmClientPoolList(CrmClientPool crmClientPool)
    {

        return crmClientPoolMapper.selectCrmClientPoolList(crmClientPool);
    }

    /**
     * 新增客户公海池
     * 
     * @param crmClientPool 客户公海池
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCrmClientPool(CrmClientPool crmClientPool)
    {
        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        if (StringUtils.isNotNull(sysUser.getNickName())){
            crmClientPool.setCreateBy(sysUser.getNickName());
        }else {
            crmClientPool.setCreateBy("该用户未设置名字");
        }
        crmClientPool.setCreateTime(DateUtils.getNowDate());

        crmClientPool.setClientVestInId(SecurityUtils.getUserId());
        crmClientPool.setClientStatus(ClientStatusEnum.BEINCHARGE.getCode());
        crmClientPool.setClientVestIn(sysUser.getNickName());
        crmClientPool.setKeyboarderId(SecurityUtils.getUserId());
        crmClientPool.setKeyboarder(sysUser.getNickName());

        int rows = crmClientPoolMapper.insertCrmClientPool(crmClientPool);
        insertCrmClientPhone(crmClientPool);
        return rows;
    }

    /**
     * 修改客户公海池
     * 
     * @param crmClientPool 客户公海池
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCrmClientPool(CrmClientPool crmClientPool)
    {
        crmClientPool.setUpdateTime(DateUtils.getNowDate());
        crmClientPoolMapper.deleteCrmClientPhoneByClientId(crmClientPool.getClientId());
        insertCrmClientPhone(crmClientPool);
        return crmClientPoolMapper.updateCrmClientPool(crmClientPool);
    }

    /**
     * 批量删除客户公海池
     * 
     * @param clientIds 需要删除的客户公海池主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCrmClientPoolByClientIds(Long[] clientIds)
    {
        crmClientPoolMapper.deleteCrmClientPhoneByClientIds(clientIds);
        return crmClientPoolMapper.deleteCrmClientPoolByClientIds(clientIds);
    }

    /**
     * 删除客户公海池信息
     * 
     * @param clientId 客户公海池主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCrmClientPoolByClientId(Long clientId)
    {
        crmClientPoolMapper.deleteCrmClientPhoneByClientId(clientId);
        return crmClientPoolMapper.deleteCrmClientPoolByClientId(clientId);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param crmClientPool 客户公海池对象
     */
    public void insertCrmClientPhone(CrmClientPool crmClientPool)
    {
        List<CrmClientPhone> crmClientPhoneList = crmClientPool.getCrmClientPhoneList();
        Long clientId = crmClientPool.getClientId();
        if (StringUtils.isNotNull(crmClientPhoneList))
        {
            List<CrmClientPhone> list = new ArrayList<CrmClientPhone>();
            for (CrmClientPhone crmClientPhone : crmClientPhoneList)
            {
                crmClientPhone.setClientId(clientId);
                list.add(crmClientPhone);
            }
            if (list.size() > 0)
            {
                crmClientPoolMapper.batchCrmClientPhone(list);
            }
        }
    }
}
